package com.yicong.ants.ui.video;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseStatefulActivity;
import com.dueeeke.videoplayer.player.VideoView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.common.MultiItemWrapper;
import com.yicong.ants.bean.video.VideoListBean;
import com.yicong.ants.databinding.VideoListActivityBinding;
import com.yicong.ants.ui.video.adapter.VideoListAdapter;
import com.yicong.ants.ui.video.manager.ViewPagerLayoutManager;
import com.yicong.ants.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class VideoListActivity extends BaseStatefulActivity<VideoListActivityBinding> implements View.OnClickListener {
    RecyclerView mRecyclerView;
    ViewPagerLayoutManager mViewPagerLayoutManager;
    int mPosition;
    VideoListAdapter mAdapter;
    VideoView mVideoView;
    String mUid;
    String mType;

    @Override
    protected int getLayout() {
        return R.layout.video_list_activity;
    }

    @Override
    protected void initEventAndData() {
        StatusBarUtil.setStatusImmerAndTextWhite(getWindow());
        mDataBind.topField.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0,0);
        mDataBind.setClick(this);
        mRecyclerView = mDataBind.recycler;
        mPosition = getIntent().getIntExtra(Constants.Extra.VIDEO_LIST_INDEX, 0);
        mType = getIntent().getStringExtra(Constants.Extra.Type);
        mUid = getIntent().getStringExtra(Constants.Extra.Uid);

        initAdapter();


        List<VideoListBean> videoHomeListBeanList = JsonHelper.toList(getIntent().getStringExtra(Constants.Extra.VIDEO_LIST), VideoListBean.class);
        List<MultiItemWrapper> list = new ArrayList<>();
        if (CollectionHelper.isNotEmpty(videoHomeListBeanList)) {
            for (VideoListBean videoListBean : videoHomeListBeanList) {
                list.add(MultiItemWrapper.of(VideoListAdapter.NORMAL_VIDEO, videoListBean));
            }
        }
        mAdapter.setNewData(list);
        mViewPagerLayoutManager.scrollToPosition(mPosition);
        mAdapter.loadMoreComplete();
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        if (event.getCode()==Constants.Event.Video_Del_Update){
            finish();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    @Override
    protected void onLoadData() {
    }

    void initAdapter() {
        mAdapter = new VideoListAdapter(this, thisActivity, mDisposable)
            .setType(mType)
            .setUserId(mUid);
        mRecyclerView.setLayoutManager(mViewPagerLayoutManager = mAdapter.getViewPagerLayoutManager());
        mAdapter.bindToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(mAdapter);
        mVideoView = mAdapter.mVideoView;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.close) {
            finish();
        }
    }
}
